//
//  NaviBarUtil.m
//
//  Created by ladaka31 ladaka on 13. 5. 31..
//  Copyright (c) 2013년 ladaka Inc. All rights reserved.
//

#import "NaviBarUtil.h"

@implementation NaviBarUtil
@synthesize navController=_navController;
@synthesize backgroundImage=_backgroundImage;
@synthesize navItem=_navItem;
@synthesize navBarLeftBtn = m_navBarLeftBtn;
@synthesize navBarRightBtn = m_navBarRightBtn;


- (id)initWithNavigationController:(UINavigationController *)navController navigationItem:(UINavigationItem *)navItem {
    self = [super init];
    if (self) {
        self.navController = navController;
        self.navController.title = @"";
        self.navItem = navItem;
    }
    
    return self;
}


//IOS 5.0이상
- (void) naviBarSetting:(NSString *)title;
{
    if ([self.navController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"ad_header_bar.png"] forBarMetrics:UIBarMetricsDefault];
    }
    if (title != nil) {
        self.navItem.title = title;
        
        UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(85,0, 150, 44)];
        lbl.backgroundColor =[UIColor clearColor];
        lbl.text= title;
        lbl.textColor=[UIColor whiteColor];
        lbl.textAlignment = NSTextAlignmentCenter;
        //    lbl.shadowColor = [UIColor blackColor];
        //    lbl.shadowOffset = CGSizeMake(0, 1);
        lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size: 17.0];
        lbl.adjustsFontSizeToFitWidth=YES;
        self.navItem.titleView=lbl;
    }
}

//IOS 5.0이상
- (void) naviBarSetting:(NSString *)title barImageUrl:(NSString*)imageUrl;
{
    if ([self.navController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navController.navigationBar setBackgroundImage:[UIImage imageNamed:imageUrl] forBarMetrics:UIBarMetricsDefault];
    }
    if (title != nil) {
        self.navItem.title = title;
        
        UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(85,0, 150, 44)];
        lbl.backgroundColor =[UIColor clearColor];
        lbl.text= title;
        lbl.textColor=[UIColor blackColor];
        lbl.textAlignment = NSTextAlignmentCenter;
        //    lbl.shadowColor = [UIColor blackColor];
        //    lbl.shadowOffset = CGSizeMake(0, 1);
        lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size: 18.0];
        lbl.adjustsFontSizeToFitWidth=YES;
        self.navItem.titleView=lbl;
    }
}

- (void) naviBarTitle:(NSString *)title {
    if (title != nil) {
        self.navItem.title = title;
        
        UILabel *lbl=[[UILabel alloc] initWithFrame:CGRectMake(85,0, 60, 44)];
        lbl.backgroundColor =[UIColor clearColor];
        lbl.text= title;
        lbl.textColor=[UIColor whiteColor];
        lbl.textAlignment = NSTextAlignmentCenter;
        //    lbl.shadowColor = [UIColor blackColor];
        //    lbl.shadowOffset = CGSizeMake(0, 1);
        lbl.font = [UIFont fontWithName:@"Helvetica-Bold" size: 17.0];
        lbl.adjustsFontSizeToFitWidth=YES;
        self.navItem.titleView=lbl;
    }
}


/**
 네비게이션바 왼쪽 버튼 구현
 @param normalImage 
 @param highlightImage
*/
- (void)addLeftButtonNormalImage:(UIImage *) normalImage highlightImage:(UIImage *) highlightImage
                                    target:(id)target action:(SEL)action
{
    // normalImage가 nil이면 버튼이 삭제된다.
    if (normalImage == nil) {
        self.navController.navigationItem.leftBarButtonItem = nil;
        return;
    }
    
    CGRect rect = CGRectMake(0, 0, normalImage.size.width/2, normalImage.size.height/2);
    
    self.navBarLeftBtn = [[UIButton alloc] initWithFrame:rect];
    [self.navBarLeftBtn setImage:normalImage forState:UIControlStateNormal];
    [self.navBarLeftBtn setImage:highlightImage forState:UIControlStateHighlighted];
    [self.navBarLeftBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    self.navItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.navBarLeftBtn];
}


/**
 네비게이션바 오른쪽 버튼 구현
 @param normalImage 
 @param highlightImage
 */
- (void)addRightButtonNormalImage:(UIImage *) normalImage highlightImage:(UIImage *) highlightImage
                          target:(id)target action:(SEL)action
{
    // normalImage가 nil이면 버튼이 삭제된다.
    if (normalImage == nil) {
        self.navController.navigationItem.rightBarButtonItem = nil;
        return;
    }
    
    CGRect rect = CGRectMake(0, 0, normalImage.size.width/2, normalImage.size.height/2);
    
    self.navBarRightBtn = [[UIButton alloc] initWithFrame:rect];
    [self.navBarRightBtn setImage:normalImage forState:UIControlStateNormal];
    [self.navBarRightBtn setImage:highlightImage forState:UIControlStateHighlighted];
    [self.navBarRightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    self.navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.navBarRightBtn];
}


/**
 네비게이션바 오른쪽에 버튼 두 개가 들어갈 경우
 @param nomarl, highlight, secNormal, secHighlight, target, action, secAction
 */
- (void) addRightBarButton:(UIImage *) normal highlight:(UIImage *) highlight secNormal:(UIImage *)secNormal secHighlight:(UIImage *) secHighlight target:(id)target firstAct:(SEL)action secondAction:(SEL)secAction {
    
    self.navBarRightBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, normal.size.width/2, normal.size.height/2)];
    self.navBarLeftBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, secNormal.size.width/2, secNormal.size.height/2)];

    [self.navBarRightBtn setImage:normal forState:UIControlStateNormal];
    [self.navBarRightBtn setImage:highlight forState:UIControlStateHighlighted];
    [self.navBarRightBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    [self.navBarLeftBtn setImage:secNormal forState:UIControlStateNormal];
    [self.navBarLeftBtn setImage:secHighlight forState:UIControlStateHighlighted];
    [self.navBarLeftBtn addTarget:target action:secAction forControlEvents:UIControlEventTouchUpInside];
    
    
    UIBarButtonItem *item0 = [[UIBarButtonItem alloc] initWithCustomView:self.navBarRightBtn];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:self.navBarLeftBtn];
    NSArray *arr = [[NSArray alloc] initWithObjects:item0, item1, nil];
    
    self.navItem.rightBarButtonItems = arr;
}


@end
